class Student:
    def __init__(self, name: str):
        self.name = name
        self.subjects = []

    def enroll(self, subject):
        if subject not in self.subjects:
            self.subjects.append(subject)
            subject.enroll_student(self)

    def __repr__(self):
        return f"Student(name={self.name})"


class Subject:
    def __init__(self, title: str):
        self.title = title
        self.instructors = []
        self.students = []

    def enroll_student(self, student: Student):
        if student not in self.students:
            self.students.append(student)

    def add_instructor(self, instructor):
        if instructor not in self.instructors:
            self.instructors.append(instructor)
            instructor.teach_subject(self)

    def __repr__(self):
        return f"Subject(title={self.title})"


class Instructor:
    def __init__(self, name: str, department: 'Department'):
        self.name = name
        self.department = department
        self.subjects = []

    def teach_subject(self, subject: Subject):
        if subject not in self.subjects:
            self.subjects.append(subject)
            subject.add_instructor(self)  # 添加指导教师到课程

    def __repr__(self):
        return f"Instructor(name={self.name}, department={self.department.name})"


class Department:
    def __init__(self, name: str):
        self.name = name
        self.instructors = []
        self.subjects = []

    def add_instructor(self, instructor: Instructor):
        if instructor not in self.instructors:
            self.instructors.append(instructor)

    def add_subject(self, subject: Subject):
        if subject not in self.subjects:
            self.subjects.append(subject)

    def __repr__(self):
        return (f"Department(name={self.name}, "
                f"instructors={self.instructors}, subjects={self.subjects})")


class School:
    def __init__(self):
        self.departments = []

    def add_department(self, department: Department):
        if department not in self.departments:
            self.departments.append(department)

    def __repr__(self):
        return f"School(departments={self.departments})"


# 示例使用
if __name__ == "__main__":
    # 创建学院
    school = School()

    # 创建系
    cs_department = Department("Computer Science")
    math_department = Department("Math")

    # 添加系到学院
    school.add_department(cs_department)
    school.add_department(math_department)

    # 创建指导教师
    instructor1 = Instructor("A", cs_department)
    instructor2 = Instructor("B", math_department)

    # 添加指导教师到系
    cs_department.add_instructor(instructor1)
    math_department.add_instructor(instructor2)

    # 创建课程
    subject1 = Subject("数据结构")
    subject2 = Subject("线性代数")

    # 添加课程到系
    cs_department.add_subject(subject1)
    math_department.add_subject(subject2)

    # 指导教师教授课程
    instructor1.teach_subject(subject1)
    instructor2.teach_subject(subject2)

    # 创建学生
    student1 = Student("a")
    student2 = Student("b")

    # 学生选课
    student1.enroll(subject1)
    student1.enroll(subject2)
    student2.enroll(subject1)

    print(school)
